//#include "stdafx.h"
#include "FrameProfile.h"

#ifdef LEVEL_EDITOR
//#include "IrrCompileGameConfig.h"
#include "Irrlicht.h"
#endif

#ifdef FRAME_PROFILE_ENABLE

#include "Core/Commons.h"

bool FrameProfiler::s_dump = false;
bool FrameProfiler::s_inFlushBatch = false;
int FrameProfiler::s_maxDrawCall = 0;
int FrameProfiler::s_drawCallFrameCounter = 0;
int FrameProfiler::s_drawCallSum = 0;

std::vector<SDrawcallInfo> FrameProfiler::s_drawCallInfosBatch;
std::vector<SDrawcallInfo> FrameProfiler::s_drawCallInfos;

void FrameProfiler::beginFrame()
{
	s_drawCallInfosBatch.clear();
	s_drawCallInfos.clear();	
}

void FrameProfiler::endFrame()
{
	s_drawCallSum += s_drawCallInfos.size();
	s_drawCallFrameCounter++;

	if(s_dump)
	{
		dumpDrawCallInfo();
		s_dump = false;
	}
}

void FrameProfiler::addDrawCallInfoBatch(const char* textureName, int triangleCount, int vertexCount)
{
	SDrawcallInfo info;
	info.TextureName = textureName;
	info.TriangleCount = triangleCount;
	info.VertexCount = vertexCount;	
	info.IsBatch = true;
	s_drawCallInfosBatch.push_back(info);
}

void FrameProfiler::addDrawCallInfo(const char* textureName, int triangleCount, int vertexCount)
{
	SDrawcallInfo info;
	info.TextureName = textureName;
	info.TriangleCount = triangleCount;
	info.VertexCount = vertexCount;	
	info.IsBatch = s_inFlushBatch;
	s_drawCallInfos.push_back(info);
}

void FrameProfiler::dumpDrawCallInfo()
{
	//dai_yx
	/*
	int drawCallCount = s_drawCallInfos.size();
	if(drawCallCount>s_maxDrawCall)
		s_maxDrawCall = drawCallCount;


	DEBUG_OUT("--------------dump draw call info of last frame--------------");
	DEBUG_OUT("Total draw call count=%d (include batch) >>>",drawCallCount);
	int i=0;
	for(std::vector<SDrawcallInfo>::const_iterator iter=s_drawCallInfos.begin(); iter!=s_drawCallInfos.end(); ++iter)
	{
		DEBUG_OUT("%d\tTexture=%s\t\t\tTriangleCount=%d\tVertexCount=%d\t[IsBatch=%d]",i,(*iter).TextureName.c_str(),(*iter).TriangleCount,(*iter).VertexCount,(*iter).IsBatch);
		i++;
	}

	DEBUG_OUT("\nBatch draw call count=%d >>>",s_drawCallInfosBatch.size());
	i=0;
	for(std::vector<SDrawcallInfo>::const_iterator iter=s_drawCallInfosBatch.begin(); iter!=s_drawCallInfosBatch.end(); ++iter)
	{
		DEBUG_OUT("%d\tTexture=%s\t\t\tTriangleCount=%d\tVertexCount=%d",i,(*iter).TextureName.c_str(),(*iter).TriangleCount,(*iter).VertexCount);
		i++;
	}
	DEBUG_OUT("-------------------------------------------------------------");
	DEBUG_OUT("Max draw call =%d, average draw call since last dump=%f",s_maxDrawCall,(float)s_drawCallSum/s_drawCallFrameCounter);
	DEBUG_OUT("-------------------------------------------------------------");

	s_drawCallSum = 0;
	s_drawCallFrameCounter = 0;

	//dump first level empty scene nodes
	Application*				app		= Application::instance();
	irr::IrrlichtDevice*		device	= app->GetDevice();
	irr::scene::ISceneManager*	smgr	= device->getSceneManager();
	const irr::core::list<irr::scene::ISceneNode*>& children = smgr->getRootSceneNode()->getChildren();
	irr::core::list<irr::scene::ISceneNode*>::ConstIterator iter = children.begin();
	int c=0;
	for(;iter!=children.end();++iter)
	{
		//if((*iter)->getType()==irr::scene::ESNT_EMPTY && (*iter)->isVisible() && (*iter)->getChildren().getSize()==0)
		//if((*iter)->isVisible())
		{
			irr::scene::ESCENE_NODE_TYPE type = (*iter)->getType();

			DEBUG_OUT("node: name=%s, id=%d, type=%c%c%c%c, visible=%d",(*iter)->getName(),(*iter)->getID(),
					type&0x000000ff,(type&0x0000ff00)>>8,(type&0x00ff0000)>>16,(type&0xff000000)>>24, (*iter)->isVisible());
		}
	}
	*/
}

#endif //FRAME_PROFILE_ENABLE
